package com.ruoyi.dataMain.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.baseMain.domain.vo.BaseAccountVo;
import com.ruoyi.baseMain.service.IBaseAccountService;
import com.ruoyi.baseMain.service.IBaseCustomService;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.dataMain.domain.bo.DataConsumptionOriginalBo;
import com.ruoyi.dataMain.domain.dto.JlxhConsumptionOriginalDto;
import com.ruoyi.dataMain.domain.dto.TxConsumptionOriginalDto;
import com.ruoyi.dataMain.service.IDataConsumptionOriginalService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

/**
 * 消耗总表腾讯tx文件自定义导入监听器
 *
 * @author 林子恒
 */
@Slf4j
public class DataConsumptionJlxhImportListener extends AnalysisEventListener<JlxhConsumptionOriginalDto> implements ExcelListener<JlxhConsumptionOriginalDto> {

    private final IDataConsumptionOriginalService iDataConsumptionOriginalService;
    private final String operName;
    private final IBaseAccountService iBaseAccountService;
    private final IBaseCustomService iBaseCustomService;

    private String filePath;
    private String bitch;
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public DataConsumptionJlxhImportListener(String filePath,String bitch) {
        this.filePath = filePath;
        this.bitch = bitch;
        this.iDataConsumptionOriginalService = SpringUtils.getBean(IDataConsumptionOriginalService.class);
        this.iBaseCustomService = SpringUtils.getBean(IBaseCustomService.class);
        this.iBaseAccountService = SpringUtils.getBean(IBaseAccountService.class);
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void invoke(JlxhConsumptionOriginalDto jlxhConsumptionOriginalDto, AnalysisContext context) {
        DataConsumptionOriginalBo dataConsumptionOriginal = BeanUtil.toBean(jlxhConsumptionOriginalDto, DataConsumptionOriginalBo.class);
        try {
            // 验证是否存在这个用户

            ValidatorUtils.validate(dataConsumptionOriginal);
            dataConsumptionOriginal.setCreateBy(operName);
            dataConsumptionOriginal.setPath(filePath);
            dataConsumptionOriginal.setBitch(bitch);
            if(dataConsumptionOriginal.getAccountId() != null){
                BaseAccountVo baseAccount = iBaseAccountService.queryByAccountId(dataConsumptionOriginal.getAccountId());
                if(baseAccount != null){
                    if(baseAccount.getCompanyName() != null && !"".equals(baseAccount.getCompanyName())){
                        dataConsumptionOriginal.setCompanyName(baseAccount.getCompanyName());
                    }
                    if(baseAccount.getSignatory() != null && !"".equals(baseAccount.getSignatory())){
                        dataConsumptionOriginal.setSignatory(baseAccount.getSignatory());
                    }
                    if(baseAccount.getCustomName() != null && !"".equals(baseAccount.getCustomName())){
                        dataConsumptionOriginal.setCustomName(baseAccount.getCustomName());
                    }
                    if(baseAccount.getBusinessUnitName() != null && !"".equals(baseAccount.getBusinessUnitName())){
                        dataConsumptionOriginal.setBusinessUnitName(baseAccount.getBusinessUnitName());
                    }
                    if(baseAccount.getAccountBelong() != null && !"".equals(baseAccount.getAccountBelong())){
                        dataConsumptionOriginal.setSupplierName(baseAccount.getAccountBelong());
                    }
                }
            }
            //包含千川 就是千川
            //巨量本地推   本地推
            //巨量ad
            if(jlxhConsumptionOriginalDto.getSourceName().contains("千川")){
                dataConsumptionOriginal.setSignatory("巨量千川");
            }else if(jlxhConsumptionOriginalDto.getSourceName().contains("巨量本地推")){
                dataConsumptionOriginal.setSignatory("巨量本地推");
            }else{
                dataConsumptionOriginal.setSignatory("巨量AD");
            }

            //消耗人员
            if(!Objects.isNull(dataConsumptionOriginal.getCustomName())){
                String saleName = iBaseCustomService.getSaleUserByCustomName(dataConsumptionOriginal.getCustomName());
                if(!Objects.isNull(saleName)){
                    dataConsumptionOriginal.setSaleUser(saleName);
                }
            }



            iDataConsumptionOriginalService.insertByBo(dataConsumptionOriginal);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、帐户名称 ").append(dataConsumptionOriginal.getAccountName()).append(" 导入成功");
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、帐户名称 " + dataConsumptionOriginal.getAccountName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<JlxhConsumptionOriginalDto> getExcelResult() {
        return new ExcelResult<JlxhConsumptionOriginalDto>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<JlxhConsumptionOriginalDto> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
